package com.yanqu.road.server.cmd.cross3.xrank;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_XRANK_GET_REWARD_FROM_CROSS, desc = "领取排行奖励 跨服返回排名")
public class GetXRankRewardFromCrossCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;
    private int myRank;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.GetXRankRewardFromCrossRespMsg reqMsg = XRankProto.GetXRankRewardFromCrossRespMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        myRank = reqMsg.getMyRank();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME;
            return;
        }

        List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getActivityConditionInfoListByType(activityId, conditionType);
        if (conditionInfoList.isEmpty()) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        eRankInfo rankInfo = eRankInfo.getERankInfo(activityInfo.getType(), activityInfo.getChildType(), conditionType);
        if (rankInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_REWARD_NO_ORDER; // 随便弄的错误码
            return;
        }

        ret = player.getModule(ActivityModule.class).getXRankReward(activityId, conditionType, myRank, rankInfo);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            XRankProto.GetXRankRewardRespMsg.Builder builder = XRankProto.GetXRankRewardRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_XRANK_GET_REWARD, builder);
        }
    }
}
